import Vue from 'vue';
var formatTime = require("../components/fotmatTime.js");
var Validform_v5 = require("../libs/Validform_v5.3.2_min.js");
var viewMode = require("./mainVueView.js");
var bootstrapModals = require("../vendors/bootstrap-modals.js");
var $cardInfo, $rateSelect;
var rateArray;
var rateSelectObject;
var cardInfoForm;
var card_6th;
var infoData = {
    totalNum: "",
    commodityName: "",
    orderNum: "",
    payDate: "",
    rateArray: {}
};



$(document).ready(function() {

    initValue();
    viewMode.initView();
    cardInfoForm = $cardInfo.Validform({
        tiptype: 2,
        datatype: {
            "zh2-15": /^[\u4E00-\u9FA5\uf900-\ufa2d]{2,8}$/,
            "sfz": /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/,
            "yxq": /([0][1-9]|[1][0-2])\d{2}/
        }
    });
    getRateInfo();
    bindEvent();
});


// 拼接请求参数
function getParameters() {
    var userName = $("#user-name").val();
    var userId = $("#user-id").val().replace(/ /g, "");
    var CardNum = $("#credit-card").val().replace(/ /g, "");
    var parameters = [{
        name: "phoneNo",
        value: $("#user-phone").val().replace(/ /g, "")
    }, {
        name: "creditName",
        value: userName
    }, {
        name: "idno",
        value: userId
    }, {
        name: "creditNo",
        value: CardNum
    }, {
        name: "cvn2",
        value: $("#cvn2Num").val()
    }, {
        name: "exp",
        value: $("#time-num").val()
    }, {
        name: "servlet_type",
        value: "create_order"
    }, {
        name: "verifyCode",
        value: $("#verify-code").val()
    }, {
        name: "orderId",
        value: $("#orderNum").text()
    }, {
        name: "sign",
        value: $("#signs").val()
    }, {
        name: "tookenkey",
        value: $("#tookenkey").val()
    }]; //请求参数

    card_6th = CardNum.substr(CardNum.length - 6); // 传到付款码页面用于重新生成串码
    return parameters;
}


// 请求支付返回数据回调函数
function successCallBack(zfdata) {
    console.dir(zfdata);
    //$('#loding').modal('hide');
    zfdata = JSON.parse(zfdata);

    var errorCode;
    if (zfdata.error_business_response && zfdata.error_business_response.code) {
        errorCode = zfdata.error_business_response.code;
        // 支付成功返回码位000，其他均为支付失败
        if (errorCode == "0000") { // 
            viewMode.toResult();

        } else if ("50,51,52,53".search(errorCode) != -1) {
            /*  pview.showErroPrompt(zfdata.error_business_response.msg);
              pview.showCardInfo();*/

        } else {
            /*
                        var pmsg = changeErrorMsg(errorCode);
                        if (pmsg == "" || pmsg == undefined) {
                            pmsg = "该笔订单支付失败";
                        }
                        ucookies.setCookie("pmsg", "该笔订单支付失败");

                        location.href = "../../installment/pcinctallment/prompt.html?msg=" + pmsg;*/
        }
    } else {

    }

}

function pay() {
    var paramet =  getParameters();
    $.post(payInstallmentServlet, paramet, successCallBack);
}


// 确认支付
function submitInfoClick() {
    var checkFormVal = cardInfoForm.check(false);
    console.log(checkFormVal);
    if (checkFormVal) {
        pay();
    }
}

function initValue() {
    $cardInfo = $("#cardInfo");
    $rateSelect = $("#rateSelect");
    var nowTime = new Date();
    var timeStr = formatTime.format1(nowTime);
    var $realTime = new Vue({
        el:"#real-time",
        data:{
            timeStr: timeStr
        }
    });

    setInterval(function  () {
        nowTime.setTime(nowTime.getTime() + 1000);
        timeStr = formatTime.format1(nowTime);
        $realTime.timeStr = timeStr;
    },1000);
}

// 取分期信息
function getRateInfo() {
    $.post(payInstallmentInfoServlet, {"orderId":"123456675"}, infocallback);
}

// 分期信息返回接口
function infocallback(infoBackData) {
    console.dir(infoBackData);
    infoData = JSON.parse(infoBackData);
    $("#payLoading").hide();
    $(".wrapper").show();
    if (infoData && infoData.orderNum) {
        var OrderInfoObject = new Vue({
            el: '#orderInfo',
            data: infoData
        });
        rateSelectObject = new Vue({
            el: "#countBox",
            data: {
                selected: '6',
                rateArray: infoData.rateArray,
                rateObject: infoData.rateArray[6]
            },
            methods: {
                changeRate: function  () {
                    this.rateObject = this.rateArray[this.selected];
                },
                toFillUserInfo:viewMode.toFillUserInfo
            }
        });
        rateArray = infoData.rateArray;
    }

}

// 绑定事件
function bindEvent() {
    $("#submitInfo").on("click", submitInfoClick);
}